﻿//-----------------------------------------------------------------------
// <copyright file="EmailTemplate.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Delta.Mercury.Business.Web
{
    using System;
    using System.Data;
    using System.Xml.Linq;
    using Delta.Data.DataExpress;
    using Delta.Mercury.Business.Exceptions;

    /// <summary>
    /// Email template
    /// </summary>
    [Serializable]
    public class EmailTemplate : IdentifierBase, IDataServicable
    {
        #region Members
        /// <summary>
        /// Email template display name
        /// </summary>
        private string name;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the EmailTemplate class.
        /// </summary>
        public EmailTemplate()
            : this(string.Empty, string.Empty, string.Empty)
        {
        }

        /// <summary>
        /// Initializes a new instance of the EmailTemplate class.
        /// </summary>
        /// <param name="name">Email template name</param>
        /// <param name="subject">Email subject</param>
        /// <param name="body">Email body</param>
        public EmailTemplate(string name, string subject, string body)
        {
            this.name = string.Empty;
            this.Data = new XDocument(new XDeclaration("1.0", "UTF-16", null), new XElement("Email", new XElement("Subject"), new XElement("Body")));
            this.Type = EmailType.AccountActivation;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the email template name
        /// </summary>
        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                if (value.Length < 1)
                {
                    throw new InvalidLengthException("Email template name must be a minimum of 1 characters.");
                }

                if (value.Length < 50)
                {
                    throw new InvalidLengthException("Email template name can't be greater than 50 characters.");
                }

                this.name = value.ToUpper();
            }
        }

        /// <summary>
        /// Gets or sets the email template xml data
        /// </summary>
        public XDocument Data { get; set; }

        /// <summary>
        /// Gets or sets the email template type
        /// </summary>
        public EmailType Type { get; set; }
        #endregion

        #region Load Methods
        /// <summary>
        /// Load email template from database
        /// </summary>
        /// <param name="guid">Unique idnetifier</param>
        /// <returns>Filled email template</returns>
        public static EmailTemplate Load(Guid guid)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", guid));
            return DataAccess.GetSingle<EmailTemplate>("LoadEmailTemplate", CommandType.StoredProcedure, parameters);
        }

        /// <summary>
        /// Load email template from database
        /// </summary>
        /// <param name="type">Email Type</param>
        /// <returns>Filled email template</returns>
        public static EmailTemplate Load(EmailType type)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Type", type));
            return DataAccess.GetSingle<EmailTemplate>("LoadEmailTemplate", CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Get Body Method
        /// <summary>
        /// Gets the body from the xdocument
        /// </summary>
        /// <returns>Body string</returns>
        public string GetBody()
        {
            return this.Data.Element("Email").Value;
        }
        #endregion

        #region Get Subject Method
        /// <summary>
        /// Gets the subject from the xdocument
        /// </summary>
        /// <returns>Subject string</returns>
        public string GetSubject()
        {
            return this.Data.Element("Email").Value;
        }
        #endregion

        #region Set Body Method
        /// <summary>
        /// Sets the body string
        /// </summary>
        /// <param name="body">Body string</param>
        public void SetBody(string body)
        {
            this.Data.Element("Email").Value = body;
        }
        #endregion

        #region Set Subject Method
        /// <summary>
        /// Set the subject string
        /// </summary>
        /// <param name="subject">Subject string</param>
        public void SetSubject(string subject)
        {
            this.Data.Element("Email").Value = subject;
        }
        #endregion

        #region Save Method
        /// <summary>
        /// Save email template to database
        /// </summary>
        public void Save()
        {
            DataAccess.NonQuery("SaveEmailTemplate", CommandType.StoredProcedure, GetSaveParameters());
        }
        #endregion

        #region Delete Method
        /// <summary>
        /// Delete email template from database
        /// </summary>
        public void Delete()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            DataAccess.NonQuery("DeleteEmailTemplate", CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Fill Method
        /// <summary>
        /// Fill the object from the datasource
        /// </summary>
        /// <param name="reader">Data source</param>
        public void Fill(IDataReader reader)
        {
            this.Data = XDocument.Parse(Helper.ToString(reader["Data"]));
            this.name = Helper.ToString(reader["Name"]);
            this.Guid = Helper.ToGuid(reader["Guid"]);
            this.Type = Helper.ToEnum<EmailType>(reader["Type"]);
        }
        #endregion

        #region Get Save Parameters
        /// <summary>
        /// Get save parameters
        /// </summary>
        /// <returns></returns>
        public Parameters GetSaveParameters()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            parameters.Add(new Parameter("Name", this.name));
            parameters.Add(new Parameter("Data", this.Data.ToString()));
            parameters.Add(new Parameter("Type", this.Type));
            return parameters;
        }
        #endregion
    }
}
